<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理 - AI面试系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
    <div class="container">
        <a class="navbar-brand" href="#">AI面试系统 - 用户管理</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav ms-auto">
                <li class="nav-item">
                    <span class="nav-link">欢迎，[[${#authentication?.principal?.username ?: '管理员'}]]</span>
                </li>
                <li class="nav-item">
                    <a class="nav-link" th:href="@{/admin/users}">用户管理</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" th:href="@{/interview}">返回系统</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" th:href="@{/logout}">退出登录</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

<div class="container mt-4">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2><i class="bi bi-people"></i> 用户管理</h2>
                <a th:href="@{/admin/users/create}" class="btn btn-primary">
                    <i class="bi bi-plus-circle"></i> 添加用户
                </a>
            </div>

            <!-- 消息提示 -->
            <div th:if="${successMessage}" class="alert alert-success alert-dismissible fade show" role="alert">
                [[${successMessage}]]
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>

            <div th:if="${errorMessage}" class="alert alert-danger alert-dismissible fade show" role="alert">
                [[${errorMessage}]]
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>

            <!-- 用户列表 -->
            <div class="card shadow">
                <div class="card-body">
                    <div th:if="${users.isEmpty()}" class="text-center py-5">
                        <i class="bi bi-person-x display-1 text-muted"></i>
                        <h4 class="text-muted mt-3">暂无用户</h4>
                        <p class="text-muted">点击上方按钮添加第一个用户</p>
                    </div>

                    <div th:unless="${users.isEmpty()}" class="table-responsive">
                        <table class="table table-hover">
                            <thead class="table-light">
                            <tr>
                                <th>ID</th>
                                <th>用户名</th>
                                <th>全名</th>
                                <th>邮箱</th>
                                <th>角色</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr th:each="user : ${users}">
                                <td>[[${user.id}]]</td>
                                <td>
                                    <strong>[[${user.username}]]</strong>
                                </td>
                                <td>[[${user.fullName}]]</td>
                                <td>[[${user.email}]]</td>
                                <td>
                                    <span th:if="${user.role.name() == 'ADMIN'}" class="badge bg-danger">管理员</span>
                                    <span th:if="${user.role.name() == 'USER'}" class="badge bg-primary">普通用户</span>
                                </td>
                                <td>
                                    <div class="btn-group" role="group">
                                        <a th:href="@{/admin/users/edit/{id}(id=${user.id})}" 
                                           class="btn btn-sm btn-outline-primary" title="编辑">
                                            <i class="bi bi-pencil"></i>
                                        </a>
                                        <a th:href="@{/admin/users/change-password/{id}(id=${user.id})}" 
                                           class="btn btn-sm btn-outline-warning" title="修改密码">
                                            <i class="bi bi-key"></i>
                                        </a>
                                        <button type="button" class="btn btn-sm btn-outline-danger" 
                                                th:onclick="'deleteUser(' + ${user.id} + ')'" title="删除">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </div>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 删除确认模态框 -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">确认删除</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                确定要删除这个用户吗？此操作不可撤销。
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="deleteForm" method="post" style="display: inline;">
                    <button type="submit" class="btn btn-danger">删除</button>
                </form>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
<script>
    function deleteUser(userId) {
        const deleteForm = document.getElementById('deleteForm');
        deleteForm.action = '/admin/users/delete/' + userId;
        const deleteModal = new bootstrap.Modal(document.getElementById('deleteModal'));
        deleteModal.show();
    }
</script>
</body>
</html>
